<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>onlinehandler.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_onlinehandler.js.html">onlinehandler.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This event handler will dispatch events when
<a name="line17"></a> * {@code navigator.onLine} changes.  HTML5 defines two events, online and
<a name="line18"></a> * offline that is fired on the window.  As of today 3 browsers support these
<a name="line19"></a> * events: Firefox 3 (Gecko 1.9), Opera 9.5, and IE8.  If we have any of these
<a name="line20"></a> * we listen to the &#39;online&#39; and &#39;offline&#39; events on the current window
<a name="line21"></a> * object.  Otherwise we poll the navigator.onLine property to detect changes.
<a name="line22"></a> *
<a name="line23"></a> * Note that this class only reflects what the browser tells us and this usually
<a name="line24"></a> * only reflects changes to the File -&gt; Work Offline menu item.
<a name="line25"></a> *
<a name="line26"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line27"></a> * @see ../demos/onlinehandler.html
<a name="line28"></a> */
<a name="line29"></a>
<a name="line30"></a>// TODO(arv): We should probably implement some kind of polling service and/or
<a name="line31"></a>// a poll for changes event handler that can be used to fire events when a state
<a name="line32"></a>// changes.
<a name="line33"></a>
<a name="line34"></a>goog.provide(&#39;goog.events.OnlineHandler&#39;);
<a name="line35"></a>goog.provide(&#39;goog.events.OnlineHandler.EventType&#39;);
<a name="line36"></a>
<a name="line37"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line38"></a>goog.require(&#39;goog.events.BrowserFeature&#39;);
<a name="line39"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line40"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line41"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line42"></a>goog.require(&#39;goog.net.NetworkStatusMonitor&#39;);
<a name="line43"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * Basic object for detecting whether the online state changes.
<a name="line49"></a> * @constructor
<a name="line50"></a> * @extends {goog.net.NetworkStatusMonitor}
<a name="line51"></a> */
<a name="line52"></a>goog.events.OnlineHandler = function() {
<a name="line53"></a>  goog.base(this);
<a name="line54"></a>
<a name="line55"></a>  this.eventHandler_ = new goog.events.EventHandler(this);
<a name="line56"></a>
<a name="line57"></a>  // Some browsers do not support navigator.onLine and therefore we don&#39;t
<a name="line58"></a>  // bother setting up events or timers.
<a name="line59"></a>  if (!goog.events.BrowserFeature.HAS_NAVIGATOR_ONLINE_PROPERTY) {
<a name="line60"></a>    return;
<a name="line61"></a>  }
<a name="line62"></a>
<a name="line63"></a>  if (goog.events.BrowserFeature.HAS_HTML5_NETWORK_EVENT_SUPPORT) {
<a name="line64"></a>    var target =
<a name="line65"></a>        goog.events.BrowserFeature.HTML5_NETWORK_EVENTS_FIRE_ON_BODY ?
<a name="line66"></a>        document.body : window;
<a name="line67"></a>    this.eventHandler_.listen(target,
<a name="line68"></a>        [goog.events.EventType.ONLINE, goog.events.EventType.OFFLINE],
<a name="line69"></a>        this.handleChange_);
<a name="line70"></a>  } else {
<a name="line71"></a>    this.online_ = this.isOnline();
<a name="line72"></a>    this.timer_ = new goog.Timer(goog.events.OnlineHandler.POLL_INTERVAL_);
<a name="line73"></a>    this.eventHandler_.listen(this.timer_, goog.Timer.TICK, this.handleTick_);
<a name="line74"></a>    this.timer_.start();
<a name="line75"></a>  }
<a name="line76"></a>};
<a name="line77"></a>goog.inherits(goog.events.OnlineHandler, goog.net.NetworkStatusMonitor);
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * Enum for the events dispatched by the OnlineHandler.
<a name="line82"></a> * @enum {string}
<a name="line83"></a> */
<a name="line84"></a>goog.events.OnlineHandler.EventType = goog.net.NetworkStatusMonitor.EventType;
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * The time to wait before checking the {@code navigator.onLine} again.
<a name="line89"></a> * @type {number}
<a name="line90"></a> * @private
<a name="line91"></a> */
<a name="line92"></a>goog.events.OnlineHandler.POLL_INTERVAL_ = 250;
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * Stores the last value of the online state so we can detect if this has
<a name="line97"></a> * changed.
<a name="line98"></a> * @type {boolean}
<a name="line99"></a> * @private
<a name="line100"></a> */
<a name="line101"></a>goog.events.OnlineHandler.prototype.online_;
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * The timer object used to poll the online state.
<a name="line106"></a> * @type {goog.Timer}
<a name="line107"></a> * @private
<a name="line108"></a> */
<a name="line109"></a>goog.events.OnlineHandler.prototype.timer_;
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Event handler to simplify event listening.
<a name="line114"></a> * @type {goog.events.EventHandler}
<a name="line115"></a> * @private
<a name="line116"></a> */
<a name="line117"></a>goog.events.OnlineHandler.prototype.eventHandler_;
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/** @override */
<a name="line121"></a>goog.events.OnlineHandler.prototype.isOnline = function() {
<a name="line122"></a>  return goog.events.BrowserFeature.HAS_NAVIGATOR_ONLINE_PROPERTY ?
<a name="line123"></a>      navigator.onLine : true;
<a name="line124"></a>};
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * Called every time the timer ticks to see if the state has changed and when
<a name="line129"></a> * the online state changes the method handleChange_ is called.
<a name="line130"></a> * @param {goog.events.Event} e The event object.
<a name="line131"></a> * @private
<a name="line132"></a> */
<a name="line133"></a>goog.events.OnlineHandler.prototype.handleTick_ = function(e) {
<a name="line134"></a>  var online = this.isOnline();
<a name="line135"></a>  if (online != this.online_) {
<a name="line136"></a>    this.online_ = online;
<a name="line137"></a>    this.handleChange_(e);
<a name="line138"></a>  }
<a name="line139"></a>};
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Called when the online state changes.  This dispatches the
<a name="line144"></a> * {@code ONLINE} and {@code OFFLINE} events respectively.
<a name="line145"></a> * @param {goog.events.Event} e The event object.
<a name="line146"></a> * @private
<a name="line147"></a> */
<a name="line148"></a>goog.events.OnlineHandler.prototype.handleChange_ = function(e) {
<a name="line149"></a>  var type = this.isOnline() ?
<a name="line150"></a>      goog.net.NetworkStatusMonitor.EventType.ONLINE :
<a name="line151"></a>      goog.net.NetworkStatusMonitor.EventType.OFFLINE;
<a name="line152"></a>  this.dispatchEvent(type);
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/** @override */
<a name="line157"></a>goog.events.OnlineHandler.prototype.disposeInternal = function() {
<a name="line158"></a>  goog.events.OnlineHandler.superClass_.disposeInternal.call(this);
<a name="line159"></a>  this.eventHandler_.dispose();
<a name="line160"></a>  delete this.eventHandler_;
<a name="line161"></a>  if (this.timer_) {
<a name="line162"></a>    this.timer_.dispose();
<a name="line163"></a>    delete this.timer_;
<a name="line164"></a>  }
<a name="line165"></a>};
</pre>


</body>
</html>
